package com.dingreading.cloud.soms.controller;

import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.CookiesUtil;
import com.dingreading.cloud.common.util.LoginAuthUtil;
import com.dingreading.cloud.common.util.RedisParam;
import com.dingreading.cloud.soms.util.redis.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/redis")
@Api(tags = "redis缓存管理")
public class RedisController {

    @Resource
    private RedisService redisService;

    @ApiOperation(value = "清空活动系统和叮叮学院的缓存")
    @PostMapping(value = "/clearStoreSubject", produces = "application/json; charset=UTF-8")
    public R<Object> clearStoreSubject() {
        redisService.setNullLike(RedisParam.MAIN_SUBJECT);
        redisService.setNullLike(RedisParam.CHILD_SUBJECT);
        redisService.setNullLike(RedisParam.MAIN_COURSETYPE);
        redisService.setNullLike(RedisParam.CHILD_COURSETYPE);
        redisService.setNullLike(RedisParam.MAIN_TABLEDOWNLOADTYPE);
        redisService.setNullLike(RedisParam.CHILD_TABLEDOWNLOADTYPE);
        redisService.setNullLike(RedisParam.DXH_SUBJECT);
        redisService.setNullLike(RedisParam.HBJD_SUBJECT);
        redisService.setNullLike(RedisParam.HBJD_LESSON);

        return R.ok();
    }

    @ApiOperation(value = "清空加盟店的数据缓存")
    @PostMapping(value = "/clearStoreData", produces = "application/json; charset=UTF-8")
    public R<Object> clearStoreData() {
        redisService.setNullLike(RedisParam.STORE_DATA);
        redisService.setNullLike(RedisParam.STORE_DATA_SERVICE);
        redisService.setNullLike(RedisParam.STORE_DATA_KINDS);
        redisService.setNullLike(RedisParam.STORE_MEMBERS);
        redisService.setNullLike(RedisParam.STORE_CHANNEL);
        redisService.setNullLike(RedisParam.STORE_SOURCE_CHANNEL);
        redisService.setNullLike(RedisParam.CORP_TEACHER);
        redisService.setNullLike(RedisParam.PROJECT_CYCLE);

        return R.ok();
    }


    @ApiOperation(value = "清空所有登录员工的缓存信息")
    @PostMapping(value = "/clearAllStaffInfo", produces = "application/json; charset=UTF-8")
    public R<Object> clearAllStaffInfo() {
        redisService.setNullLike(RedisParam.STAFF_LOGIN);

        return R.ok();
    }

    @ApiOperation(value = "清空当前登录员工的缓存信息")
    @PostMapping(value = "/clearStaffInfo", produces = "application/json; charset=UTF-8")
    public R<Object> clearStaffInfo(
            HttpServletRequest request
    ) {
        Cookie cookie = CookiesUtil.getCookieByName(request, LoginAuthUtil.DDPB_TOKEN);
        if (cookie != null && StringUtils.isNotBlank(cookie.getValue())) {
            String cookieValue = cookie.getValue();
            redisService.setNullLike(RedisParam.STAFF_LOGIN + cookieValue);
        }

        return R.ok();
    }

}